
#ifndef EXT2_H
#define EXT2_H

#define SIZE_SEC 512

#define N_EXT2_NUMERO_MAGICO 0xEF53 //for normal versione of ext2
#define P_EXT2_NUMERO_MAGICO 0xEF51 //for versions of ext2fs prior to 0.2b.

#define EXT2_ERRORS_CONTINUE 1 
#define EXT2_ERRORS_RO 2 
#define EXT3_ERRORS_PANIC 3 

#define EXT2_OS_LINUX 0
#define EXT2_OS_HURD 1
#define EXT2_MASIX 2
#define EXT2_FREEBSD 3
#define EXT2_OS_LITES4 4

#define EXT2_GOOD_OLD_REV 0 //original format
#define EXT2DYNAMIC_REV 1 

#define EXT2_BAD_INO 0x01
#define EXT2_ROOT_INO 0x02       
#define EXT2_ACL_IDX_INO 0x03    
#define EXT2_ACL_DARA_INO 0x04   
#define EXT2_BOOTLOADER INO 0x05 
#define EXT2_UNDEL_DIR_INO 0x06

// ------------ file format ------------
#define MODE_MASK 0xF000 // format mask
#define MODE_SOCK 0xC000 // socket
#define MODE_LINK 0xA000 // symbolic link
#define MODE_FILE 0x8000 // regular file
#define MODE_BDEV 0x6000 // block device
#define MODE_DIR  0x4000 // directory
#define MODE_CDEV 0x2000 // character device
#define MODE_FIFO 0x1000 // fifo

// ------------ access rights ------------
#define EXT2_S_ISUID 0x0800 // SUID
#define EXT2_S_ISGID 0x0400 // SGID
#define EXT2_S_ISVTX 0x0200 // sticky bit
#define EXT2_S_IRWXU 0x01C0 // user access rights mask
#define EXT2_S_IRUSR 0x0100 // read
#define EXT2_S_IWUSR 0x0080 // write
#define EXT2_S_IXUSR 0x0040 // execute
#define EXT2_S_IRWXG 0x0038 // group access right mask
#define EXT2_S_IRGRP 0x0020 // read
#define EXT2_S_IWGRP 0x0010 // write
#define EXT2_S_IXGRP 0x0008 // execute
#define EXT2_S_IRWXO 0x0007 // others access rights mask
#define EXT2_S_IROTH 0x0004 // read
#define EXT2_S_IWOTH 0x0003 // write
#define EXT2_S_IXOTH 0x0001 // execute


//------------ tipo file ---------------
#define EXT2_FT_UNKNOWN 0
#define EXT2_FT_REG_FILE 1
#define EXT2_FT_DIR 2
#define EXT2_FT_CHRDEV 3
#define EXT2_FT_BLKDEV 4
#define EXT2_FT_FIFO 5
#define EXT2_FT_SOCK 6
#define EXT2_FT_SYMLINK 7
#define EXT2_FT_MAX 8


#define DIM_SUPER_BLOCK 1024
#define START_SUPER_BLOCK 1024

struct super_block{
  u32int s_inodes_count;
  u32int s_blocks_count;
  u32int s_r_blocks_count;
  u32int s_free_blocks_count; 
  u32int s_free_inodes_count; 
  u32int s_first_data_block;
  u32int s_log_block_size;
  u32int s_log_frag_size;

  u32int s_blocks_per_group;  
  u32int s_frags_per_group;   
  u32int s_inodes_per_group;  
  u32int s_mtime;             
  u32int s_wtime;             
  u16int s_mnt_count; 
  u16int s_max_mnt_count; 
  u16int s_magic;
  u16int s_state;
  u16int s_errors;          
  u16int s_minor_rev_level; 
  u32int s_lastcheck;       
  u32int s_checkinterval;   
  u32int s_creator_os;      
  u32int s_rev_level;       
  u16int s_def_resuid;      
  u16int s_def_resgid;      

  u32int s_first_ino;
  u16int s_inode_size;
  u16int s_block_group_nr;    
  u32int s_feature_compact;   
  u32int s_feature_incompact; 
  u32int s_feature_ro_compact;
  u8int  s_uuid[16];          
  u16int s_volume_name;       
  u8int  s_last_mounted[8];  
  u32int s_algo_bitmap;
  u8int  s_reserved[886];     
};


struct group_descriptor{
  u32int bg_block_bitmap;       
  u32int bg_inode_bitmap;       
  u32int bg_inode_table;        
  u16int bg_free_blocks_count;  
  u16int bg_free_inodes_count;  
  u16int bg_used_dirs_count;    
  u16int bg_pad;                
  u32int bg_reserved[3];        
};


struct i_node{
  u16int i_mode;
  u16int i_uid;
  u32int i_size; 
  u32int i_atime; 
  u32int i_ctime; 
  u32int i_mtime; 
  u32int i_dtime; 
  u16int i_gid; 
  u16int i_links_count;
  u32int i_blocks;
  u32int i_flags;
  u32int i_osd1; 
  u32int i_block[15];
  u32int i_generation;
  u32int i_file_acl;
  u32int i_dir_acl;
  u32int i_faddr;
  
  /* inode osd2 structure Linux */
  u8int  l_i_frag;
  u8int  l_i_fsize;
  u16int reserved1;
  u16int l_i_uid_high;
  u16int l_i_gid_high;
  u32int reserved2;
};

struct dir_ff{
  u32int inode;
  u16int rec_len;
  u8int  name_len;
  u8int  file_type;
  char name[1];
};

/* Funkcijos */
boolean init_ext2();
char *pwd_ext2();
void ls_ext2();
void cd_ext2(char *);
void cat_ext2(char *);
void ext2();

#endif /* EXT2_H */
